package com.danger.Util.ImageUtil;

import com.danger.Activity.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.view.View;
import android.widget.ImageView;


/**
 * Created by danger on 2016/4/5.
 */
public class ImageLoaderUtil {

    private ImageLoader imageLoader;
    private ImageSize imageSize;
    private ImageView imageView;
    private CollapsingToolbarLayout layout;
    private String url;

    public ImageLoaderUtil(ImageSize imageSize, ImageView imageView, String url,Context context) {
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        this.imageSize = imageSize;
        this.imageView = imageView;
        this.url = url;
    }

    public ImageLoaderUtil( CollapsingToolbarLayout layout, String url, Context context) {
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        this.layout = layout;
        this.url = url;
    }


    public void load(){
        imageView.setTag(url);
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.loading)
                .showImageOnFail(R.mipmap.no_photo)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        imageLoader.loadImage(url, imageSize, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view,
                                          Bitmap loadedImage) {
                super.onLoadingComplete(imageUri, view, loadedImage);
                imageView.setImageBitmap(loadedImage);
            }
        });
    }

    public void loadLayoutBackGround(){
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.loading)
                .showImageOnFail(R.mipmap.no_photo)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        imageLoader.loadImage(url,options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view,
                                          Bitmap loadedImage) {
                super.onLoadingComplete(imageUri, view, loadedImage);
                layout.setBackground(new BitmapDrawable(loadedImage));
            }
        });
    }
}
